#!/usr/bin/python
# -*- coding: UTF-8 -*-
#cron:6 6,8,10 * * *
import json
import time
import random
import requests
import base64
import os
import sys
token=os.environ.get('hipc_token')


# 加载通知服务
def load_send():
    cur_path = os.path.abspath(os.path.dirname(__file__))
    sys.path.append(cur_path)
    if os.path.exists(cur_path + "/notify2.py"):
        try:
            from notify2 import send
            return send
        except Exception as e:
            print(f"加载通知服务失败：{e}")
            return None
    else:
        print("加载通知服务失败")
        return None
#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
import base64
import json
#"bonus_key":"laxin"
my_dict = {"bonus_key":"laxin"}
json_str = json.dumps(my_dict).encode('utf-8')
base64_str = base64.b64encode(json_str).decode('utf-8')
payload=base64.b64decode("eyJib251c19rZXkiOiAiYWR2ZXJ0aXNpbmcifQ==")
payload1=base64.b64decode("eyJib251c19rZXkiOiJhZHZlcnRpc2luZyJ9")

# 看广告3次每天，{"bonus_key":"advertising"}，eyJib251c19rZXkiOiJhZHZlcnRpc2luZyJ9
#拉新已做完99次，{"bonus_key":"laxin"}，eyJib251c19rZXkiOiAibGF4aW4ifQ==
#打开PC应用3次，{"bonus_key":"open_pc_app"},
#远程截屏1次，{"bonus_key":"screen"},
#远程操作电脑2次,{"bonus_key":"open_computer"}，
#发消息给电脑2次，{"bonus_key":"send_text_to_computer"},
#使用PPT助手1次，{"bonus_key":"ppt"},
#换壁纸1次，{"bonus_key":"wallpaper"},
#晒配置1次，{"bonus_key":"create_computer_code"}
#同步云书签1次，{"bonus_key":"bookmark"},
#上传文件到电脑3次,{"bonus_key":"upload_file_computer"}
#远程屏幕同步1次，{"bonus_key":"screen_synchronization"},
#分享文件给好友3次，{"bonus_key":"new_share_file"},
print(base64_str)
print(payload)
print(payload1)
'''

def pick():
    headers = {
     'content-length':'2',
     'user-id':'41222',
     'xweb_xhr':'1',
     'platform':'miniprogram_windows',
     'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11437',
     'app':'hipc',
     'token':token,
     'content-type':'application/json',
     'accept':'*/*',
     'sec-fetch-site':'cross-site',
     'sec-fetch-mode':'cors',
     'sec-fetch-dest':'empty',
     'referer':'https://servicewechat.com/wx6265a35adb332603/180/page-frame.html',
     'accept-encoding':'gzip, deflate, br',
     'accept-language':'zh-CN,zh;q=0.9'}
    payload=base64.b64decode("e30=")

    response0 = requests.request("POST", "https://api.hipcapi.com/v1/scorebonus/pickup", headers=headers, data=payload)

def canuse():
    headers = {
        'content-length': '2',
        'user-id': '41222',
        'xweb_xhr': '1',
        'platform': 'miniprogram_windows',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11437',
        'app': 'hipc',
        'token': token,
        'content-type': 'application/json',
        'accept': '*/*',
        'sec-fetch-site': 'cross-site',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://servicewechat.com/wx6265a35adb332603/180/page-frame.html',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9'}
    payload = base64.b64decode("e30=")
    response0 = requests.request("POST", "https://api.hipcapi.com/v1/userscore/detail", headers=headers, data=payload)
    kongjian = response0.json()["data"]["can_use_score"]
    return kongjian
def encode_dict(dictionary):
    """
    对字典进行Base64编码操作
    :param dictionary: 要编码的字典
    :return: 编码后的Base64字节串
    """
    # 将字典转换为JSON字符串并编码为字节串
    json_str = json.dumps(dictionary).encode('utf-8')
    # 进行Base64编码
    encoded_dict = base64.b64encode(json_str)
    return encoded_dict


# 定义要编码的字典列表
dictionaries = [
    {"bonus_key": "advertising"},
    {"bonus_key": "open_pc_app"},
    {"bonus_key": "screen"},
    {"bonus_key": "open_computer"},
    {"bonus_key": "send_text_to_computer"},
    {"bonus_key": "ppt"},
    {"bonus_key": "wallpaper"},
    {"bonus_key": "create_computer_code"},
    {"bonus_key": "bookmark"},
    {"bonus_key": "upload_file_computer"},
    {"bonus_key": "screen_synchronization"},
    {"bonus_key": "new_share_file"}
]

# 用于存储编码结果的字典
encoded_results = {}


def chickin(data):
    headers = {
        'content-length': '27',
        'user-id': '41222',
        'xweb_xhr': '1',
        'platform': 'miniprogram_windows',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11437',
        'app': 'hipc',
        'token': token,
        'content-type': 'application/json',
        'accept': '*/*',
        'sec-fetch-site': 'cross-site',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://servicewechat.com/wx6265a35adb332603/180/page-frame.html',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9'}
    payload = base64.b64decode(data)
    # data = {"bonus_key":"advertising"}

    response0 = requests.request("POST", "https://api.hipcapi.com/v1/scorebonus/finish", headers=headers, data=payload)
    print(response0.text)
    content = response0.json()["message"]
    return content
def runchickin():
    canuse1 = canuse()
    contents = []
    for dictionary in dictionaries:
        encoded_result = encode_dict(dictionary)
        data= encoded_result.decode('utf-8')
        data2 = json.dumps(data)
        #chickin(data)
        number = random.randint(50,300)
        print(f'延迟{number}秒')
        time.sleep(number)
        content=f'签到结果：{chickin(data)}\n'
        contents.append(content)
        print(content)
        #content
            #data1=base64.b64decode(encoded_result)
            #print(data)
            #print(data1)
    pick()
    canuse2 = canuse()
    content = f'之前空间：{canuse1}M\n现在空间：{canuse2}M\n本次共获得：{canuse2-canuse1}M空间'
    contents.append(content)
    #print(content)
    contents_str = "".join(contents)
    return contents_str

# 将编码结果字典转换为JSON字符串
#data = json.dumps(encoded_results)

#print(data)
title = "HIPC签到通知"
contents = runchickin()
print(contents)
send_notify = load_send()
send_notify(title, contents)
# 将编码结果字典转换为JSON字符串
#data = json.dumps(encoded_results)

#print(data)